package main

import (
	"fmt"
	"sync"
	"time"
)

var wg sync.WaitGroup

func fn1(ch chan<- int) {
	defer close(ch)
	for i := 0; i < 10; i++ {
		ch <- i
		fmt.Println("当前写入数据:", i)
		time.Sleep(time.Millisecond * 50)
	}

	wg.Done()
}

func fn2(ch <-chan int) {
	for v := range ch {
		fmt.Println("当前读取数据:", v)
		time.Sleep(time.Millisecond * 50)
	}
	wg.Done()
}

// 单向管道
func main() {
	// 默认是双向管道
	var a = make(chan int, 2)
	a <- 1
	a <- 2
	ar1 := <-a
	ar2 := <-a
	fmt.Println(ar2)
	fmt.Println(ar1)

	// 单向管道
	ch2 := make(chan<- int, 2)
	ch2 <- 10
	ch2 <- 12

	// 只写管道
	ch3 := make(<-chan int, 2)
	fmt.Println(ch3)

	ch4 := make(chan int, 10)

	wg.Add(1)
	go fn1(ch4)
	wg.Add(1)
	go fn2(ch4)

	wg.Wait()
	fmt.Println("执行完毕....")
}
